package me.luger.core.commom.init;

import me.luger.core.commom.util.StringUtil;
import me.luger.core.mvc.WebService;
import me.luger.core.mvc.entity.RequestEntity;
import me.luger.core.mvc.filter.pagecache.annotation.PageCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;

/**
 * Created by Administrator on 2016/8/18 0018.
 */
@Component
public class InitWebService implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Autowired
    private Environment env;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        initPageCachUrlMapping();
        initProjectProperties();

    }

    /**
     * 初始化项目相关参数
     */
    private void initProjectProperties() {
        String basePath = env.getProperty("project.basePath");
        if(StringUtil.isNotBlank(basePath)){
            WebService.setProjectBasePath(basePath);
        }

        String domain = env.getProperty("project.domain");
        if(StringUtil.isNotBlank(domain)){
            WebService.setProjectDomain(domain);
        }

        String profile = env.getProperty("project.profile");
        if(StringUtil.isNotBlank(profile)){
            WebService.setProfileId(profile);
        }

    }


    /**
     * 初始化uri和pagecache注解的对应关系
     */
    private void initPageCachUrlMapping() {
        Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {

            RequestMappingInfo info = m.getKey();
            HandlerMethod handlerMethod = m.getValue();
            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                if (url == null || url.isEmpty()) {
                    continue;
                }
                put2UrlMapping(url,handlerMethod,info);
                put2PageCacheUrlMapping(url,handlerMethod);
            }
        }
    }

    private void put2UrlMapping(String url,HandlerMethod handlerMethod,RequestMappingInfo info){

        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setUrl(url);
        requestEntity.setClassName(handlerMethod.getMethod().getDeclaringClass().getName()); // 类名
        requestEntity.setMethod(handlerMethod.getMethod().getName()); // 方法名
        String name = info.getName();
        requestEntity.setName(name);
        RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
        String type = methodsCondition.toString();
        if (type != null && type.startsWith("[") && type.endsWith("]")) {
            type = type.substring(1, type.length() - 1);
            requestEntity.setType(type); // 方法名
        }else{
            requestEntity.setType("GET");//默认都为get方法
        }
        WebService.putUrlMapping(requestEntity);
    }

    private void put2PageCacheUrlMapping(String url,HandlerMethod handlerMethod){
        PageCache pageCache = handlerMethod.getMethodAnnotation(PageCache.class);
        if(null != pageCache){
            WebService.putPageCacheUrlMapping(url,pageCache);
        }
    }
}
